package cn.yanjr.system.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.yanjr.common.bean.BeanFactory;
import cn.yanjr.system.dao.SysDictDao;
import cn.yanjr.system.entity.SysDict;
import cn.yanjr.system.service.SysDictService;

public class SysDictServiceImpl implements SysDictService {

	private SysDictDao sysDictDao = BeanFactory.getBean("sysDictDao", SysDictDao.class);

	@Override
	public List<SysDict> findAllType(){
		List<SysDict> list = null;
		try {
			list = sysDictDao.findAllType();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<SysDict> findByType(String type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SysDict get(String id){
		SysDict dict = null;
		try {
			dict = sysDictDao.findOne(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dict;
	}

	@Override
	public int save(SysDict task) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(String id){
		int c = 0;
		try {
			c = sysDictDao.delete(id, 1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return c;
	}

	@Override
	public int update(SysDict task) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map<String, Object> queryPage(Map<String, Object> param) {
		//param
		int pageNo = Integer.parseInt(param.get("pageNo").toString());
		int pageSize = Integer.parseInt(param.get("pageSize").toString());
		String label = param.get("label") == null ? "" : param.get("label").toString();
		String type = param.get("type") == null ? "" : param.get("type").toString();
		String description = param.get("description") == null ? "" : param.get("description").toString();
		String remarks = param.get("remarks") == null ? "" : param.get("remarks").toString();
		
		//return
		Map<String, Object> map = new HashMap<String, Object>();
		
		//sql
		StringBuilder sql = new StringBuilder();
		sql.append("select * from sys_dict where del_flag=0 ");
		if(StringUtils.isNotEmpty(label)) {
			sql.append("and label like '%").append(label).append("%' ");
		}
		if(StringUtils.isNotEmpty(type)) {
			sql.append("and type='").append(type).append("' ");
		}
		if(StringUtils.isNotEmpty(description)) {
			sql.append("and description  like '%").append(description).append("%' ");
		}
		if(StringUtils.isNotEmpty(remarks)) {
			sql.append("and remarks like '%").append(remarks).append("%' ");
		}
		sql.append(" order by type, sort, create_date");
		map = sysDictDao.findEntityPage(sql.toString(), pageNo, pageSize);
		map.put("code", 0);
		map.put("msg", "查询成功！");
		
		return map;
	}


}
